import java.util.ArrayList;
//import java.util.LinkedList;
import java.util.List;

class PrimeCalculator {
    private List<Integer> primes;

    PrimeCalculator() {
        this.primes = new ArrayList<>();
//        this.primes = new LinkedList<>(); // not faster than ArrayList ?
        this.primes.add(2);
        this.primes.add(3); // It's important.
    }

    int nth(int inputNumber) {
        if (inputNumber <= 0) {
            throw new IllegalArgumentException();
        }

        if ((inputNumber - 1) < this.primes.size()) {
            return this.primes.get(inputNumber - 1);
        }

        // first is 3.
        int p = this.primes.get(this.primes.size() - 1) + 2;
        while (true) {
            boolean isPrimes = true;
            for (Integer prime : this.primes) {
                if (p % prime == 0) {
                    isPrimes = false;
                }
            }

            if (isPrimes) {
                this.primes.add(p);
                break;
            }

            p = p + 2;
        }

        return this.nth(inputNumber);
    }
}